﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Controllers;
using Infrastructure;
using Infrastructure.DataTypes;
namespace RecommenderSystem
{
    //new delegate to allow passing the userID argument
    public delegate void ButtonSetEventHandler(User<Discrete> A_userID);

    public partial class LoadUser : Form
    {
        //event when the button set user is clicked
        public event ButtonSetEventHandler ButtonClicked;
        User<Discrete> active_user;
        Dispatcher data;
        CreateUserWithScores m_frm;
        public LoadUser()
        {
            InitializeComponent();
            // Set up the ToolTip text for the Button and Checkbox.
            
        }

        public LoadUser(Dispatcher data)
        {
            InitializeComponent();
            this.data = data;
            //This is because the default celltype is string. When sorting, this will cause comparing string to sorting. But actual value type doesn’t match to string.
            dataGridView1.Columns[0].CellTemplate.ValueType = typeof(double);
            dataGridView1.Columns[1].CellTemplate.ValueType = typeof(double);
            dataGridView1.Columns[2].CellTemplate.ValueType = typeof(double);
            dataGridView1.Columns[0].ValueType = typeof(double);
            dataGridView1.Columns[1].ValueType = typeof(double);
            dataGridView1.Columns[2].ValueType = typeof(double);
            LoadData();
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            // Set up the ToolTip text for the Button and Checkbox.
            toolTip1.SetToolTip(this.buttonRndUch, "Select user ID randommly");
            toolTip1.SetToolTip(this.dataGridView1, "Select user ID from list");
        }


        private void buttonRndUch_Click(object sender, EventArgs e)
        {
            active_user = data.PickRandomUser();
            labelChosenU.Text = "Chosen User :" + active_user.UserID;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[0].Value.ToString() == active_user.UserID)
                {
                    row.Selected = true;
                    dataGridView1.FirstDisplayedCell = row.Cells[0];
                    break;
                }
            }
        }

        private void LoadData()
        {
            try
            {
                List<User<Discrete>> users = data.UsersList().ToList();
                dataGridView1.Rows.Clear();
                foreach (User<Discrete> user in users)
                {
                    DataGridViewRow temp=new DataGridViewRow();
                    temp.CreateCells(dataGridView1);
                    
                    double[] row = new double[3];
                    temp.Cells[0].Value = System.Convert.ToDouble(user.UserID);
                    if (!double.IsNaN(user.StatisticsAttributes.VoteCount))
                    {
                        temp.Cells[1].Value = user.StatisticsAttributes.VoteCount;
                    }
                    if (!double.IsNaN(user.StatisticsAttributes.AverageRating))
                    {
                        temp.Cells[2].Value = Math.Round(user.StatisticsAttributes.AverageRating, MidpointRounding.AwayFromZero);
                    }
                    

                    this.dataGridView1.Rows.Add(temp);
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void buttonSet_Click(object sender, EventArgs e)
        {
            //raise the event and pass the arguments
            if (ButtonClicked != null)
            {
                ButtonClicked(active_user);
            }
            this.Close();
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count==1)
            {
                active_user = data.GetUser(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                labelChosenU.Text = "Chosen User :" + active_user.UserID;
            }
        }


        private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
        {
            try
            {
                if (e.Column.Name == dataGridView1.Columns[0].Name)
                {
                    e.SortResult = System.Convert.ToInt64(e.CellValue1).CompareTo(System.Convert.ToInt64(e.CellValue2));
                }
                else
                {
                    e.SortResult = System.Convert.ToDouble(e.CellValue1).CompareTo(System.Convert.ToDouble(e.CellValue2));
                }
            
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
           
        }

        private void buttonNewUser_Click(object sender, EventArgs e)
        {
            m_frm = new CreateUserWithScores(data);
            m_frm.ButtonClicked+=new ButtonSetScoresEventHandler(m_frm_ButtonClicked);
            m_frm.ShowDialog();
            LoadData();

        }
        /// <summary>
        /// show data after generatting of user
        /// </summary>
        /// <param name="active_user"></param>
        private void m_frm_ButtonClicked(User<Discrete> active_user)
        {
            
            //foreach (DataGridViewRow row in dataGridView1.Rows)
            //{
            //    if (row.Cells[0].Value.ToString() == active_user.UID)
            //    {
            //        row.Selected = true;
            //        dataGridView1.FirstDisplayedCell = row.Cells[0];
            //        break;
            //    }
            //}
        }
    //    private void dataGridView1_SortCompare(object sender,
    //DataGridViewSortCompareEventArgs e)
    //    {
    //        // Try to sort based on the cells in the current column.
    //        e.SortResult = System.String.Compare(
    //            e.CellValue1.ToString(), e.CellValue2.ToString());

    //        // If the cells are equal, sort based on the ID column.
    //        if (e.SortResult == 0 && e.Column.Name != "ID")
    //        {
    //            e.SortResult = System.String.Compare(
    //                dataGridView1.Rows[e.RowIndex1].Cells["ID"].Value.ToString(),
    //                dataGridView1.Rows[e.RowIndex2].Cells["ID"].Value.ToString());
    //        }
    //        e.Handled = true;
    //    }

    }
}
